package cn.gson.work.model.service;

import cn.gson.work.model.mapper.ResourceMapper;
import cn.gson.work.model.pojos.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional(rollbackFor = Exception.class)
public class ResourceService {
    @Autowired
    ResourceMapper rescourceMapper;

    //新增资源
    public void addResource(Resource resource){
        rescourceMapper.addResource(resource);
    }

    //修改资源
    public void updateResource(Resource resource){
        System.err.println("实体类："+resource.toString());
        rescourceMapper.updateResource(resource);
    }

    //查询资源
    public List<Resource> selectResource(String resName,String typeName,Integer typeId){
        return rescourceMapper.selectResource(resName,typeName,typeId);
    }

    //新增资源分类
    public void addType(Resourcetype resourceType){
        rescourceMapper.addType(resourceType);
    }

    //修改资源分类
    public void updateType(Resourcetype resourceType){
        System.err.println("实体类："+resourceType.toString());
        rescourceMapper.updateType(resourceType);
    }

    //删除资源分类
    public void deleteType(long typeId){
        rescourceMapper.deleteType(typeId);
    };

    //查询资源分类
    public List<Resourcetype> selectType(String typeName){
        return rescourceMapper.selectType(typeName);
    }

    //查询用户
    public List<User> selectUser(){
        return rescourceMapper.selectUser();
    };

    //新增资源申请
    public void addApplication(Application application){
        rescourceMapper.addApplication(application);
    };

    //修改资源申请
    public void updateApplication(Application application){
        rescourceMapper.updateApplication(application);
    };

    //新增车辆申请
    public void addAppCar(Application application){
        rescourceMapper.addAppCar(application);
    }

    //查询车辆申请
    public List<Application> selectAppCar(String appType,String appStage,String carName){
        return rescourceMapper.selectAppCar(appType,appStage,carName);
    };


    //查询资源申请
    public List<Application> selectApp(String appType,String typeName,String appStage){
        return rescourceMapper.selectApp(appType,typeName,appStage);
    };

    //新增车辆
    public void addCar(Car car){
        rescourceMapper.addCar(car);
    };

    //修改车辆
    public void updateCar(Car car){
        rescourceMapper.updateCar(car);
    };

    //删除车辆
    public void deleteCar(Integer carId){
        rescourceMapper.deleteCar(carId);
    };

    //查询车辆
    public List<Car> selectCar(){
        return rescourceMapper.selectCar();
    };

    //查询部门
    public List<Dept> selectDept(){
        return rescourceMapper.selectDept();
    };

    //归还资源
    public void updateAppBack(Application application){
        //修改车辆状态
        if(application.getAppCar()!=0 && application.getAppStage().equals("已通过")){
            rescourceMapper.updateAppCar("未借出",application.getAppCar());
        }
        rescourceMapper.updateAppBack(application);
    };

    //审批
    public void updateApprover(Application application){
        //修改车辆状态
        if(application.getAppCar()!=0 && application.getAppStage().equals("已通过")){
            rescourceMapper.updateAppCar("已借出",application.getAppCar());
        }
        rescourceMapper.updateApprover(application);
    }
}
